package com.xiaoshuidi.cloud.module.iot.dal.repository;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.mybatis.core.dataobject.BaseDO;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.iot.controller.admin.face.vo.FaceDatabasePageReq;
import com.xiaoshuidi.cloud.module.iot.dal.mapper.TbFaceDatabaseMapper;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbFaceDatabase;
import com.xiaoshuidi.cloud.module.iot.enums.FaceDatabaseMemberTypeEnum;
import com.xiaoshuidi.cloud.module.iot.enums.FaceDatabaseStatusEnum;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

/**
 * @Author Feng
 * @date 2023/6/25
 */
@Repository
public class TbFaceDatabaseRepository extends ServiceImpl<TbFaceDatabaseMapper, TbFaceDatabase> {

    public Page<TbFaceDatabase> page(FaceDatabasePageReq req) {
        Page<TbFaceDatabase> page = Page.of(req.getPageNo(), req.getPageSize());
        LambdaQueryWrapper<TbFaceDatabase> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotEmpty(req.getMemberType()), TbFaceDatabase::getMemberType, req.getMemberType())
                .ge(StrUtil.isNotEmpty(req.getCreateTimeStart()), BaseDO::getCreateTime, req.getCreateTimeStart())
                .and(StrUtil.isNotEmpty(req.getCreateTimeStart()),
                        s -> s.le(StrUtil.isNotEmpty(req.getCreateTimeStart()), BaseDO::getCreateTime, LocalDateTimeUtil.parseDate(req.getCreateTimeEnd(), "yyyy-MM-dd").plusDays(1)))
                .and(StrUtil.isNotEmpty(req.getSearch()), s -> s.like(TbFaceDatabase::getMemberName, req.getSearch())
                        .or()
                        .like(TbFaceDatabase::getIdNumber, req.getSearch())
                        .or()
                        .like(TbFaceDatabase::getPhone, req.getSearch()))
                .orderByDesc(BaseDO::getCreateTime);
        page = this.page(page, wrapper);
        return page;
    }


    public Page<TbFaceDatabase> appPage(int pageNo, int pageSize) {
        Page<TbFaceDatabase> page = Page.of(pageNo, pageSize);
        LambdaQueryWrapper<TbFaceDatabase> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbFaceDatabase::getMemberType, FaceDatabaseMemberTypeEnum.COHABITANT.getCode())
                .and(s -> s.eq(TbFaceDatabase::getRenterId, SecurityFrameworkUtils.getLoginUserId())
                        .or().eq(BaseDO::getCreator, SecurityFrameworkUtils.getLoginUserId()))
                .orderByAsc(TbFaceDatabase::getStatus)
                .orderByDesc(BaseDO::getCreateTime);
        page = this.page(page, wrapper);
        return page;
    }

    /**
     * 根据授权人名称、手机号count
     *
     * @param memberName
     * @param phone
     * @return
     */
    public long count(String memberName, String phone) {
        LambdaQueryWrapper<TbFaceDatabase> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbFaceDatabase::getMemberName, memberName)
                .eq(TbFaceDatabase::getPhone, phone);
        return count(wrapper);
    }

    /**
     * 查询失效到期人脸信息
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public List<TbFaceDatabase> invalidityFaceList(LocalDate startDate, LocalDate endDate) {
        LambdaQueryWrapper<TbFaceDatabase> wrapper = Wrappers.lambdaQuery();
        wrapper.lt(TbFaceDatabase::getValidEndDate, endDate)
                .eq(TbFaceDatabase::getStatus, FaceDatabaseStatusEnum.EFFECTIVE.getCode())
                .ge(TbFaceDatabase::getValidEndDate, startDate);
        return list(wrapper);
    }

    /**
     * 失效到期人脸
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public int invalidityFace(LocalDate startDate, LocalDate endDate, List<String> indexCodeList) {
        LambdaQueryWrapper<TbFaceDatabase> wrapper = Wrappers.lambdaQuery();
        wrapper.lt(TbFaceDatabase::getValidEndDate, endDate)
                .eq(TbFaceDatabase::getStatus, FaceDatabaseStatusEnum.EFFECTIVE.getCode())
                .in(TbFaceDatabase::getThirdFaceId, indexCodeList)
                .ge(TbFaceDatabase::getValidEndDate, startDate);
        return this.baseMapper.invalidityFace(wrapper, FaceDatabaseStatusEnum.EXPIRED.getCode());
    }


    public TbFaceDatabase renterInfo() {
        LambdaQueryWrapper<TbFaceDatabase> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbFaceDatabase::getRenterId, SecurityFrameworkUtils.getLoginUserId())
                .eq(TbFaceDatabase::getMemberType, FaceDatabaseMemberTypeEnum.RENTER.getCode());
        List<TbFaceDatabase> list = list(wrapper);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }
}
